package com.wc;

import com.Parser.FilebeatJSONKeyValueIISDeserializationSchema;
import com.Parser.FilebeatJSONKeyValueNginxDeserializationSchema;
import com.sink.IISSink;
import com.sink.NginxSink;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;

import org.apache.flink.connector.kafka.source.KafkaSource;
import org.apache.flink.connector.kafka.source.enumerator.initializer.OffsetsInitializer;
import org.apache.flink.connector.kafka.source.reader.deserializer.KafkaRecordDeserializationSchema;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.sink.SinkFunction;


/**
 * @author 谢秉均
 * @description  处理filebeat的IIS、nginx日志的数据采集
 */
public class KafkaConsumerMain {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        //nginx主题
        String topic="nginx-log";
        String topicprod="nginx-log-prod";

        //IIS主题
        String IIStopic = "IIS-log-test";
        String IIStopicprod = "IIS-log-prod";


        // 1.初始化 KafkaSource 实例
        KafkaSource<String> source = KafkaSource.<String>builder()
                .setBootstrapServers("10.52.3.34:9092,10.52.3.36:9092")   // 必填：指定broker连接信息 (为保证高可用,建议多指定几个节点)
                .setTopics(topicprod)                               // 必填：指定要消费的topic
                .setGroupId("my-group")                                 // 必填：指定消费者的groupid(不存在时会自动创建)
                .setDeserializer(KafkaRecordDeserializationSchema.of(
                        // isTrue = (true:以JSON格式解析nginx日志 false:以原日志格式 | 作为分隔符)
                        new FilebeatJSONKeyValueNginxDeserializationSchema(true) //nginx解析器
//                        new FilebeatJSONKeyValueIISDeserializationSchema()  //IIS解析器
                ))
//                .setValueOnlyDeserializer(new SimpleStringSchema())     // 必填：指定反序列化器(用来解析kafka消息数据，转换为flink数据类型)
                .setStartingOffsets(OffsetsInitializer.latest())      // 可选：指定启动任务时的消费位点（不指定时，将默认使用 OffsetsInitializer.earliest()）
                .build();


        // 2.通过 fromSource + KafkaSource 获取 DataStreamSink
        DataStreamSource<String> streamSource = env.fromSource(source, WatermarkStrategy.noWatermarks(), "Kafka Source");

        //创建JDBC的sink-sql server
        SinkFunction<String> sink = new NginxSink("mssql").GetNginxSinkString("dbatest","1234.com","10.52.4.9", "dbtest");
//        SinkFunction<String> sink = new IISSink("mssql").GetIISSinkString("dbatest","1234.com","10.52.4.9", "dbtest");

        //创建JDBC的sink-mysql
//        SinkFunction<String> sink = new IISSink("mysql").GetIISSinkString("root","1234","10.50.188.12", "test_db");
//        SinkFunction<String> sink = new NginxSink("mysql").GetNginxSinkString("root","1234","10.50.188.12", "test_db");


        //streamSource.print();  //调试
        streamSource.addSink(sink);
        //启动程序并设置JOB名称
        env.execute("SH-PROD-NGINX-MSSQL-10.52.4.9-V2");
    }
}
